//
//  TPCloudProViewController.m
//  CloudAtCostPanel
//
//  Created by leonardo on 2017/1/26.
//  Copyright © 2017年 tanpengsccd. All rights reserved.
//

#import "TPCloudProViewController.h"
#import "TPNetModel_Resources.h"
#import "TPNetModel_ListTemplates.h"
@interface TPCloudProViewController ()<UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate>
//PRO_S UIPickerView * pickerView_config;
PRO_S TPPickerViewController * pickerVC_CpuRamStorage;
PRO_S TPPickerViewController * pikerView_templates;
PRO_S TPPickerViewController * pickerView_counter;

PRO_S TPLabelTextField * labelTextField_config;
PRO_S NSArray * cpu_toChoose;
PRO_S NSArray * ram_toChoose;
PRO_S NSArray * storage_toChoose;
PRO_S NSArray * count_toChoose;
PRO_S TPNetModel_Resources_res * resources;
PRO_S TPNetModel_ListTemplates_res * templates;

PRO_S UITableView * tableView_temps;
@end

@implementation TPCloudProViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"CloudPro";
    // Do any additional setup after loading the view.
    
    UIBarButtonItem * barBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshView)];
    self.navigationItem.rightBarButtonItem = barBtn;
    
    
    _resources = [TPNetModel_Resources_res new];
    
    
    _labelTextField_config = [[TPLabelTextField alloc]initWithHorizonalLabelStr:@"Config: " textFieldPlaceHolderStr:@"CPU:RAM:Storage"];
    _pickerVC_CpuRamStorage = [[TPPickerViewController alloc]initWithTip:_labelTextField_config.textField.placeholder];
    _pickerVC_CpuRamStorage.pickerViewDelegate = self;
    _pickerVC_CpuRamStorage.pickerViewDataSource = self;
    _labelTextField_config.textField.delegate = self;
    
    [self.view addSubview:_labelTextField_config];
    
    [self getResources];
    [self getTemplates];
    
    _tableView_temps = [UITableView new];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [_labelTextField_config mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(_labelTextField_config.superview);
        make.height.mas_equalTo(HEIGHT_CELL);
    }];
    
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    switch (component) {
        case 0:
            return _cpu_toChoose.count;
            break;
        case 1:
            return _ram_toChoose.count;
            break;
        case 2:
            return _storage_toChoose.count;
            break;
        case 3:
           return  _templates.data.count;
            break;
        case 4:
            return _count_toChoose.count;
            break;
            
        default:
            return 0;
            break;
    }
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    switch (component) {
        case 0:
            return _cpu_toChoose[row];
            break;
        case 1:
            return _ram_toChoose[row];
            break;
        case 2:
            return _storage_toChoose[row];
            break;
        case 3:
            return _templates.data[row].name;
            break;
        case 4:
            return _count_toChoose[row];
            break;
            
        default:
            return 0;
            break;
    }
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    NSUInteger count_new = 0;
    switch (component) {
        case 0:
            count_new = (_resources.data.total.cpu_total - _resources.data.used.cpu_used)/[_cpu_toChoose[row] intValue];
            break;
        case 1:
            count_new = (_resources.data.total.ram_total - _resources.data.used.ram_used)/[_ram_toChoose[row] intValue];
            break;
        case 2:
            count_new = (_resources.data.total.storage_total - _resources.data.used.storage_used)/[_storage_toChoose[row] intValue];;
            break;

            
            
        default:
           
            break;
    }
    if (_count_toChoose.count > count_new) {
        NSMutableArray * mutableCount = [NSMutableArray new];
        for (int i = 1; i <= count_new; i ++) {
            [mutableCount addObject:[NSString stringWithFormat:@"%ui",i]];
        }
        _count_toChoose = mutableCount.copy;
    }
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == _labelTextField_config.textField) {
        [textField resignFirstResponder];
        [self presentViewController:_pickerVC_CpuRamStorage animated:YES completion:nil];
    }
}
#pragma mark - private
-(void)refreshView{
    [self getResources];
    [self getTemplates];
}
-(void)getResources{
    [self showHUDWithReqModel:[TPNetModel_Resources_req new] progress:nil successGetModel:^(TPBaseNetModel_res * _Nullable resModel) {
        _resources = (TPNetModel_Resources_res *)resModel;
        [self reloadPickerData];
        [_pickerVC_CpuRamStorage reloadAllComponents];
    } failureGetModel:^(TPBaseNetModel_res * _Nullable resModel) {
        [self showHudTips:resModel.error_description];
    }];
}
-(void)getTemplates{
    
    [self showHUDWithReqModel:[TPNetModel_ListTemplates_req new] progress:nil successGetModel:^(TPBaseNetModel_res * _Nullable resModel) {
        _templates = (TPNetModel_ListTemplates_res *)resModel;
        [self reloadPickerData];
//        [_pickerView_config reloadComponent:3];
        
    } failureGetModel:^(TPBaseNetModel_res * _Nullable resModel) {
        [self showHudTips:resModel.error_description];
    }];
}
- (void)reloadPickerData{
    //计算剩余 可用配额
    NSUInteger cpu = _resources.data.total.cpu_total - _resources.data.used.cpu_used;
    NSUInteger ram = _resources.data.total.ram_total - _resources.data.used.ram_used;
    NSUInteger storage = _resources.data.total.storage_total - _resources.data.used.storage_used;
    for (Temp * temp in _resources.data.temps) {
        cpu -= temp.cpu_temp;
        ram -= temp.ram_temp;
        storage -= temp.storage_temp;
    }
    //
    NSMutableArray * cpu_dataSource = [NSMutableArray new];
    NSMutableArray * ram_dataSource = [NSMutableArray new];
    NSMutableArray * storage_dataSource = [NSMutableArray new];
    for (int i = 1; i <= cpu; i ++) {
        [cpu_dataSource addObject: [NSString stringWithFormat:@"%i core",i ]];
    }
    for (int i = 512 ; i <= ram; i += 512) {
        [ram_dataSource addObject: [NSString stringWithFormat:@"%i MB",i ]];
    }
    for (int i = 10 ; i <= storage; i += 5) {
        [storage_dataSource addObject: [NSString stringWithFormat:@"%i GB",i ]];
    }
    _cpu_toChoose = cpu_dataSource.copy;
    _ram_toChoose = ram_dataSource.copy;
    _storage_toChoose = storage_dataSource.copy;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
